#coding:utf-8
import sqlite3




class sqlite_db:

    def __init__(self, db):
       
        self.xc = sqlite3.connect(db)
        
        self.xc.row_factory=sqlite3.Row
        self.cu = self.xc.cursor()




    def execsql(self, sql, p=None):
        try:
            
            if type(p) == type([]):
                self.cu.execute(sql, p)
            elif type(p) == type({}):
                self.cu.executemany(sql, p)
            else:
                self.cu.execute(sql)
            return True
        except :
            self.logss('erro:   sql:%s'%(sql,))
            return False;


    
    
    def  logss(self,logs):
        fp=open('log','a+')
        fp.write("%s \n"%logs)
        fp.close()

    def commit(self):
        self.xc.commit()




    def fetch(self, sql, num=1):
        
        if self.execsql(sql)==False:
            return False

        if num == 1:
            return self.cu.fetchone()


        if num > 1:
            return self.cu.fetchall()



    def rollback(self):
        self.xc.rollback()

    def close(self):
        self.xc.close()
    





if __name__ == "__main__":

    sqlite = sqlite_db("d:/test.s3db")
    d=sqlite.fetch("select * from  test",1)
    print d['users']
   
                    